@()(implicit session: Session)
@config.main("Lastz") {

    <div class="body-box">
        <div class="page-box">

            @config.subTitle("Lastz",detail ="This document describes installation and usage of the LASTZ sequence alignment program. LASTZ is a drop-in replacement for BLASTZ, and is backward compatible with BLASTZ's command-line syntax. That is, it supports all of BLASTZ's options but also has additional ones, and may produce slightly different alignment results. ")

            <div class="main-body">

                <form class="registration-form col-sm-12 my-form" id="form">

                    <div class="row">

                        @components.textarea("Enter target sequence", "targetText", "targetText", "getExample1()",placeholder="Paste target sequence(s) here.")

                        @components.file("Or, upload file", "targetFile", "lastz1", inputLabel = "7")

                        @components.textarea("Enter query sequences", "queryText", "queryText", "getExample2()",placeholder="Paste query sequence(s) here.")

                        @components.file("Or, upload file", "queryFile", "lastz2", inputLabel = "7")

                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-sm-3"></label>
                        <button type="button" class="btn my-btn" style="width: 200px" onclick="Search()">
                            Run</button>
                    </div>
                </form>

                <div id="result" style="display: none">
                    <hr>
                    <h5>Axt result:
                        &nbsp;<button class="btn my-btn" onclick="downloadAxt()"><i class="fa fa-download"></i>&nbsp;Download</button>
                    </h5>
                    <pre id="axtString" style="background-color: white"></pre>

                    <div id="png1">
                        <div id="download" style="margin-top: 20px">
                            <button type="button" class="btn my-btn" id="deleteButton" onclick="downloadPng1()"
                            style="margin-bottom: 10px">
                                <i class="fa fa-download"></i>&nbsp;download Parallel result file
                            </button>
                        </div>
                        <div id="charts">
                            <div>no picture result</div>
                        </div>
                    </div>

                    <div id="png2">
                        <div id="download">
                            <button type="button" class="btn my-btn" id="deleteButton" onclick="downloadPng2()"
                            style="margin-bottom: 10px">
                                <i class="fa fa-download"></i>&nbsp;download Xoy result file
                            </button>
                        </div>
                        <div id="charts1">
                            <div>no picture result</div>
                        </div>
                    </div>

                </div>

            </div>
        </div>
    </div>


    <script src="@routes.Assets.versioned("blast/js/FileSaver.min.js")" type="text/javascript"></script>

    <script>

            let base641;
            let base642;

            $(function () {
                $("#targetFile").fileinput();
                $("#queryFile").fileinput();
            })

            function Search() {
                let form = $("#form");
                if (targetCheck() && queryCheck()) {

                    let index = MyTable.prototype.runningIcon();
                    $.ajax({
                        url: "@routes.ToolsController.toolsFileRun("lastz")",
                        type: "post",
                        processData: false,
                        contentType: false,
                        data: new FormData(form[0]),
                        success: function (data) {
                            layer.close(index);
                            if (data.valid === "false") {
                                swal("Error", data.message, "error");
                                $("#result").hide()
                            } else {
                                base641 = data.base641;
                                base642 = data.base642;
                                if (base641 === "") {
                                    $("#png1").hide();
                                } else {
                                    $("#charts").html("<img src='data:image/png;base64," + data.base641 + "' style='width:100%'>");
                                    $("#png1").show();
                                }
                                if (base642 === "") {
                                    $("#png2").hide();
                                } else {
                                    $("#charts1").html("<img src='data:image/png;base64," + data.base642 + "' style='width:100%'>");
                                    $("#png2").show();
                                }
                                $("#axtString").text(data.axtString);
                                let $result = $("#result")
                                $result.show();
                                let target_top = $result.offset().top ;
                                $("html,body").animate({scrollTop: target_top}, 800);
                            }
                        }
                    });
                }
            }

            function getExample1() {
                $.post("/COADB/example/lastz/1", (data) => {
                            $("#targetText").val(data)
                        }
                )
            }

            function getExample2() {
                $.post("/COADB/example/lastz/2", (data) => {
                            $("#queryText").val(data)
                        }
                )
            }


            function downloadAxt() {
                var fileName = "all.axt";
                var content = $("#axtString").text();
                var blob = new Blob([content], {
                    type: "text/plain;charset=utf-8"
                });
                saveAs(blob, fileName)
            }

            function targetCheck() {
                var text = $("textarea[name='targetText']").val();
                var file = $("input[name='targetFile']").val();
                var b = true;
                if (!file && !text) {
                    b = false;
                    swal("Error", "Please enter sequence or upload file!", "error")
                } else if (file && text) {
                    b = false;
                    swal("Error", "Please either enter target sequence into the box or upload a file, not both!", "error")
                }
                return b
            }

            function queryCheck() {
                var text = $("textarea[name='queryText']").val();
                var file = $("input[name='queryFile']").val();
                var b = true;
                if (!file && !text) {
                    b = false;
                    swal("Error", "Please enter sequence or upload file!", "error")
                } else if (file && text) {
                    b = false;
                    swal("Error", "Please either enter query sequence into the box or upload a file, not both!", "error")
                }
                return b
            }

            function downloadPng1() {
                var fileName = "Target-Query.parallel.png";
                var byteArrays = base642binaryArray(base641);
                var blob = new Blob(byteArrays, {
                    type: "image/png"
                });
                saveAs(blob, fileName)
            }

            function base642binaryArray(content) {
                var byteCharacters = atob(content);
                var byteArrays = [];
                var sliceSize = 512;
                for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                    var slice = byteCharacters.slice(offset, offset + sliceSize);
                    var byteNumbers = new Array(slice.length);
                    for (var i = 0; i < slice.length; i++) {
                        byteNumbers[i] = slice.charCodeAt(i);
                    }
                    var byteArray = new Uint8Array(byteNumbers);
                    byteArrays.push(byteArray);
                }
                return byteArrays
            }

            function downloadPng2() {
                var fileName = "Target-Query.xoy.png";
                var byteArrays = base642binaryArray(base642);
                var blob = new Blob(byteArrays, {
                    type: "image/png"
                });
                saveAs(blob, fileName)
            }



    </script>
}